<?php

namespace app\admin\controller;


use think\Controller;
use think\Request;

//调用model,操作数据库,并取别名
use app\admin\model\Admin as AdminModel;
use app\admin\model\Shoper as ShoperModel;


class Index extends Controller
{

	//登录页面
	public function login()
	{
	    //dump(session(''));
        $webSite = WEB_SITE;
        $docRoot = DOC_ROOT;
        $staticAdmin = STATIC_ADMIN;

        //传值,渲染页面
        $this->assign([
            'staticAdmin' => $staticAdmin,
            'webSite'    =>  $webSite
        ]);
       return $this->fetch();

	}

	//处理ajax的用户名
    public function username()
    {
        //接收username的值
        $name = Request::instance()->param('username');

        //查询数据库
        $admin = AdminModel::get(['admin_name'=>"$name"]);
        $shoper = ShoperModel::get(['shop_name'=>"$name"]);

        //判断用户名是否存在数据库
        //若为管理员
        if ($admin) {

            //登录状态为1,初始化登录状态
            if ($admin->after_login == 1) {

                $update = AdminModel::where(['admin_name'=>"$name"])->update(['after_login'=>0]);

                //初始化状态失败
                if (!$update) {
                    $this->error('初始化状态失败','admin/index/login');

                } else {
                    $this->success('初始化状态成功','admin/index/username');
                }
            }

            $adminDel = $admin->admin_del;
            //伪删除
            if ($adminDel) {
                echo json_encode(['status'=>1, 'tip'=>'该用户已经不存在,请联系管理员或超级管理员']);
            } else {
                //正常管理员
                echo json_encode(['status'=>2, 'tip'=>'用户名合法']);
            }

            //若为商家
        } else if ($shoper) {

            //登录状态为1,初始化登录状态
            if ($shoper->after_login == 1) {

                $update = ShoperModel::where(['shop_name'=>"$name"])->update(['after_login'=>0]);

                //初始化状态失败
                if (!$update) {
                    $this->error('初始化状态失败','admin/index/login');

                } else {
                    $this->success('初始化状态成功','admin/index/username');
                }
            }

            //获取商家字段
            $shopDel = $shoper->shop_del;
            $shopPass = $shoper->is_pass;
            $shopActive = $shoper->is_active;
            $shopStop = $shoper->is_stop;

            //若未激活
            if (!$shopActive) {
                echo json_encode(['status'=>3, 'tip'=>'商家未激活']);

            } else if ($shopStop) {
                //若停售
                echo json_encode(['status'=>4, 'tip'=>'该商家已暂停营业,请联系管理员']);

            } else if (!$shopPass) {
                //若未审核
                echo json_encode(['status'=>5, 'tip'=>'商家未通过审核']);

            }  else if ($shopDel) {
                //若伪删除
                echo json_encode(['status'=>6, 'tip'=>'该商家不存在,请联系管理员']);
            }else {
                //商家用户名合法
                echo json_encode(['status'=>2, 'tip'=>'用户号名合法']);
            }

        }else {
            //用户名不存在
            echo json_encode(['status'=>7 , 'tip'=>'该用户没有访问权限']);
        }

    }


    //处理用户名,密码的ajax
    public function password()
    {
        //空格已经被过滤掉,不需要trim
        //param可以获取get,post,put值
        $name = Request::instance()->param('username');
        $pwd = Request::instance()->param('password');

        $admin = AdminModel::where(['admin_name'=>"$name", 'admin_del'=>0])->select();
        $shoper = ShoperModel::where(['shop_name'=>"$name", 'shop_del'=>0,'is_active'=>1,'is_pass'=>1,'is_stop'=>0])->select();

        //若为管理员,select查询要用foreach
        $resPwd = '';
        if ($admin) {
            //dump(111111);
            foreach ($admin as $val) {
                $resPwd = $val->admin_password;   //获取字段值: 查询的结果对象->字段名
            }

        } else if ($shoper) {
           // dump(22222222);
            //若为商家
            foreach ($shoper as $val) {
                $resPwd = $val->shop_password;   //获取字段值: 查询的结果对象->字段名
            }

        }

        //比对密码
        if ($resPwd != md5($pwd)) {
            echo json_encode(['status'=>8,'tip'=>'密码错误']);
        } else {
            echo json_encode(['status'=>9,'tip'=>'密码正确']);
        }
    }

    //处理登录,修改登录状态
    public function doLogin()
    {
        //获取用户名值
        $name = Request::instance()->param('username');

        //查找数据库
        $admin = AdminModel::where(['admin_name'=>"$name", 'admin_del'=>0])->select();
        $shoper = ShoperModel::where(['shop_name'=>"$name", 'shop_del'=>0,'is_active'=>1,'is_pass'=>1,'is_stop'=>0])->select();
//dump($shoper);
        //获取登录ip
        $ip1 = $_SERVER['REMOTE_ADDR'];
        if ($ip1 == '::1') {

            $ip1 = '127.0.0.1';
        }
        $ip = ip2long($ip1);

        //当前时间,datetime格式
        $currTime = date('Y-m-d H:i:s');

        //登录成功后修改登录状态
        if ($admin) {

            //获取id
            foreach ($admin as $val1) {
                $adminId = $val1->admin_id;
            }

            //修改管理员表
            $res = AdminModel::where(['admin_name'=>"$name"])->update(['after_login'=>1,'admin_login_ip'=>"$ip",'last_time'=>"$currTime"]);

        } else if ($shoper) {

            //获取商店id
            foreach ($shoper as $val1) {
                $shopId = $val1->shop_id;
            }

            //修改商家表
            $res = ShoperModel::where(['shop_name'=>"$name"])->update(['after_login'=>1,'shop_login_ip'=>"$ip",'last_time'=>"$currTime"]);
        }

        //判断是否修改成功
        if (!$res) {
            echo json_encode(['status'=>10,'tip'=>'登录失败']);
            //$this->error('登录失败','admin/index/login');

        } else {

            //录成功,存入session
            if ($admin) {
                //管理员
                session('admin_name',"$name");
                session('admin_id',$adminId);
                session('after_login','1');

            }

            if ($shoper) {
                //商家
                session('shop_name',"$name");
                session('shop_id',$shopId);
                session('after_login','1');

            }

            //dump($_SESSION);
            //跳转页面
            echo json_encode(['status'=>11,'tip'=>'登录成功']);
            //$this->success('登录成功','admin/user/list');
        }


    }
    //退出登录
    public function loginout()
    {
        //若存在管理员
        if (session('admin_name')) {

            $name = session('admin_name');

            //修改管理员表
            $res = AdminModel::where(['admin_name'=>"$name"])->update(['after_login'=>0]);

        } else if (session('shop_name')) {

            //若存在商家
            $name = session('shop_name');

            //修改商家表
            $res = ShoperModel::where(['shop_name'=>"$name"])->update(['after_login'=>0]);

        }
        //dump($res);
        //判断是否成功退出登录
        if (!$res) {

            $this->error('退出登录失败','admin/user/list');
        } else {

            //清除session
            if (session('admin_name')) {

                //管理员
                session('after_login',null);
                session('admin_id',null);
                session('admin_name',null);
            } else if (session('shop_name')){

                //商家
                session('after_login',null);
                session('shop_id',null);
                session('shop_name',null);

            }

            $this->success('退出登录成功','admin/index/login');
        }

    }

    //测试用的方法
    public function test()
    {

        dump(session('admin_name'));
        dump(session('shop_name'));
    }


}
